package org.aplombh.java.utils.multiThread.create;

import org.junit.Test;

import java.util.concurrent.*;

import static org.aplombh.java.utils.WebDownloader.downloader;

public class TestThreadPool implements Callable<Boolean> {
    private String url;
    private String name;

    @Override
    public Boolean call(){
        downloader(url, name);
        System.out.println("download:" + name);
        return true;
    }

    @Test
    public void threadPool() throws ExecutionException, InterruptedException {

        TestCallable thread1 = new TestCallable("https://www.baidu.com/img/flexible/logo/pc/result.png", "1.jpg");
        TestCallable thread2 = new TestCallable("https://www.baidu.com/img/flexible/logo/pc/result.png", "2.jpg");
        TestCallable thread3 = new TestCallable("https://www.baidu.com/img/flexible/logo/pc/result.png", "3.jpg");

        ExecutorService executorService = Executors.newFixedThreadPool(3);

        Future<Boolean> result1 = executorService.submit(thread1);
        Future<Boolean> result2 = executorService.submit(thread2);
        Future<Boolean> result3 = executorService.submit(thread3);

        Boolean resultBoolean1 = result1.get();
        Boolean resultBoolean2 = result2.get();
        Boolean resultBoolean3 = result3.get();

        System.out.println(resultBoolean1);
        System.out.println(resultBoolean2);
        System.out.println(resultBoolean3);

        executorService.shutdown();
    }
}
